package cn.ytr.project.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ytr.project.domain.Department;
import cn.ytr.project.domain.Employee;
import cn.ytr.project.service.IDepartmentService;
import cn.ytr.project.service.IEmployeeService;
import cn.ytr.project.service.impl.DepartmentServiceImpl;
import cn.ytr.project.service.impl.EmployeeServiceImpl;
import cn.ytr.project.util.StringUtil;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet{

	private static final long serialVersionUID = 4690923011858906308L;
	private IEmployeeService employeeService = new EmployeeServiceImpl();
	private IDepartmentService departmentService = new DepartmentServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String cmd = request.getParameter("cmd");
		if ("delete".equals(cmd)) {
			delete(request, response);
		} else if ("input".equals(cmd)) {
			input(request, response);
		} else if ("insertOrUpdate".equals(cmd)) {
			insertOrUpdate(request, response);
		} else if ("~~~~".equals(cmd)) {
			
		} else {
			list(request, response);
		}
	}
	
	
	/**
	 * 删除员工的处理
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		employeeService.delete(Long.valueOf(id));
		response.sendRedirect("/employee");
	}
	
	/**
	 * employeeInput 编辑员工页面提交表单的处理：员工的添加或编辑
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String deptId = request.getParameter("deptId");
		
		Employee employee = new Employee();
		employee.setName(name);
		if (StringUtil.hasLength(deptId)) {
			employee.setDeptId(Long.valueOf(deptId));
		}
		
		//判断添加还是编辑
		if (StringUtil.hasLength(id)) {
			employee.setId(Long.valueOf(id));
			employeeService.update(employee);
		} else {
			employeeService.insert(employee);
		}
		response.sendRedirect("/employee");
	}
	
	/**
	 * 显示添加或编辑员工的 input 页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void input(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		if (StringUtil.hasLength(id)) {
			Employee employee = employeeService.selectOneById(Long.valueOf(id));
			request.setAttribute("employee", employee);
		}
		List<Department> departments = departmentService.selectAllForList();
		request.setAttribute("departments", departments);
		request.getRequestDispatcher("/WEB-INF/views/employeeInput.jsp").forward(request, response);
	}
	
	/**
	 * 显示员工列表页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Employee> list = employeeService.selectAllForList();
		request.setAttribute("employees", list);
		request.getRequestDispatcher("/WEB-INF/views/employeeList.jsp").forward(request, response);
	}
	

}
